﻿<?php
//参考网址: http://database.csdn.net/page/b7075e9d-7872-4ad9-b667-d1ec58191dd2
//比如用crontab-e命令加入： */2****php/usr/local/sbin/kill-mysql-sleep-proc.php 就可以每隔2分钟检查并清除一次数据库中的死连接了

define ( 'MAX_SLEEP_TIME', 120 );
$hostname = "localhost";
$username = "root";
$password = "sa";
$connect = mysql_connect ( $hostname, $username, $password );
$result = mysql_query ( "SHOW PROCESSLIST", $connect );
while ( $proc = mysql_fetch_assoc ( $result ) ) {
	if ($proc ["Command"] == "Sleep" && $proc ["Time"] > MAX_SLEEP_TIME) {
		@mysql_query ( "KILL " . $proc ["Id"], $connect );
		echo "KILL " . $proc ["Id"] . "<Br />";
	}
}
mysql_close ( $connect );
?>
